import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import {HttpURLService} from "../../httpURL.service";

/**
 * 地表水体污染因子字典管理
 * guopeng
 * 2017.11.27
 */
export class AirPollutant {
  constructor(
    private id : string,//污染物id
    private atmospherePollutantCode : string,//大气污染因子编码
    private atmospherePollutantName : string,//大气污染因子名称
    private atmosphereOriginalPollutantCode : string,//大气污染因子原始编码
    private atmosphereUnitConcentration :string,//大气污染因子单位
    private atmosphereUnitDischarge : string,//大气污染因子排放量单位
    private atmosphereTypeConcentration : string,//值格式  浓度
    private convertFlag : string,//大气污染因子折算标识0无折算值1有折算值
    private extensionInfo : string//拓展信息
  ){}
}

@Component({
  templateUrl: './airPollutant.component.html',
  styleUrls: ['./airPollutant.component.css']
})
export class AirPollutantComponent implements OnInit{
  private atmospherePollutantCode : string;//大气污染因子编码
  private atmospherePollutantName : string;//大气污染因子名称

  public queryMessage:AirPollutant = new AirPollutant("","","","","","","","","");
  public updateMessages:AirPollutant = new AirPollutant("","","","","","","","","");
  public addMessages:AirPollutant =  new AirPollutant("","","","","","","","","");
  private list:Array<any>;
  errorMessage:string;//錯誤消息

  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");
  constructor(private http: HttpClient,public router:Router,private httpURLService: HttpURLService) {

  }
  add(){
    this.addMessages =  new AirPollutant("","","","","","","","","");
  }

  userMessage(event:AirPollutant){
    let query = new AirPollutant("","","","","","","","","");
    let update = new AirPollutant("","","","","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }
  listMessage(info:any){
    let atmospherePollutantCode = info.atmospherePollutantCode== undefined ? '' : info.atmospherePollutantCode ;
    let atmospherePollutantName = info.atmospherePollutantName== undefined ? '' : info.atmospherePollutantName;
    this.http.get(this.url+'/airPollutant/queryAirPollutantListByPage?atmospherePollutantCode='+atmospherePollutantCode+'&atmospherePollutantName='+atmospherePollutantName).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/airPollutant/addAirPollutant',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101118001'){
          this.errorMessage = "大气污染源因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118002'){
          this.errorMessage = "大气污染源因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118003'){
          this.errorMessage = "大气污染源因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118004'){
          this.errorMessage = "大气污染源因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118005'){
          this.errorMessage = "大气污染源因子折算标识为空！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/airPollutant/deleteAirPollutant/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101118006'){
        this.errorMessage = "该大气污染源因子不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/airPollutant/updateAirPollutant',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101118001'){
          this.errorMessage = "大气污染源因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118002'){
          this.errorMessage = "大气污染源因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118003'){
          this.errorMessage = "大气污染源因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118004'){
          this.errorMessage = "大气污染源因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118005'){
          this.errorMessage = "大气污染源因子折算标识为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101118006'){
          this.errorMessage = "该大气污染源因子不存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let atmospherePollutantCode = this.atmospherePollutantCode== undefined ? '' : this.atmospherePollutantCode ;
    let atmospherePollutantName = this.atmospherePollutantName== undefined ? '' : this.atmospherePollutantName;
    let current = pageNo;
    this.http.get(this.url+'/airPollutant/queryAirPollutantListByPage?atmospherePollutantCode='+atmospherePollutantCode+'&atmospherePollutantName='+atmospherePollutantName+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/airPollutant/queryAirPollutantListByPage').subscribe(data => {
      this.params = '/gn-communication/airPollutant/queryAirPollutantListByPage';
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    });
	}
}


